﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClasses.Pages;
using DomainModel;

public partial class Admin_ProductCenter_ProductList : NormalPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            QueryProducts();
        }
    }

    private void QueryProducts()
    {
        IEnumerable<Product> productList = this.LogicController.ProductManager.GetAllProducts();
        rptProduct.DataSource = productList;
        rptProduct.DataBind();
    }

    private Dictionary<int, Category> m_dictCategory;

    public Dictionary<int, Category> DictCategory
    {
        get
        {
            if (m_dictCategory == null)
            {
                m_dictCategory = this.LogicController.ProductManager.GetAllCategories().ToDictionary<Category, int>(c => c.ID.Value);
            }

            return m_dictCategory;
        }
    }


    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            this.LogicController.ProductManager.DeleteProductByID(Convert.ToInt32(e.CommandArgument));
            QueryProducts();
        }
    }

    public string GetCategoryNameByID(object input)
    {
        int id = Convert.ToInt32(input);
        return DictCategory[id].CategoryName;
    }
}
